﻿using SQLite;
using System;

namespace Games.Data
{
    public class Sorter<T> where T : Entity, new()
    {
        private readonly Func<AsyncTableQuery<T>, AsyncTableQuery<T>> order;
        public Sorter(Func<AsyncTableQuery<T>, AsyncTableQuery<T>> order)
        {
            this.order = order;
        }

        public static Sorter<T> Default { get; } = new Sorter<T>(q => q.OrderByDescending(x => x.ID));

        public AsyncTableQuery<T> Sort(AsyncTableQuery<T> query)
        {
            if(order != null)
            {
                return order(query);
            }
            return Default.Sort(query);
        }
    }
}
